package com.ruoyi.blindBoxManage.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.blindBoxManage.domain.BlindBoxProduct;
import com.ruoyi.blindBoxManage.domain.BlindBoxPublicProduct;
import com.ruoyi.blindBoxManage.domain.Box;
import com.ruoyi.blindBoxManage.mapper.BlindBoxProductMapper;
import com.ruoyi.blindBoxManage.mapper.BlindBoxPublicProductMapper;
import com.ruoyi.blindBoxManage.mapper.BlindProductMapper;
import com.ruoyi.blindBoxManage.mapper.BoxMapper;
import com.ruoyi.blindBoxManage.service.IBlindBoxProductService;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 盲盒商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Slf4j
@Service
public class BlindBoxProductServiceImpl implements IBlindBoxProductService {
    @Autowired
    private BlindBoxProductMapper blindBoxProductMapper;
    @Resource
    private BlindProductMapper blindProductMapper;
    @Resource
    private BlindBoxPublicProductMapper blindBoxPublicProductMapper;
    @Resource
    private BoxMapper boxMapper;

    /**
     * 查询盲盒商品
     *
     * @param id 盲盒商品主键
     * @return 盲盒商品
     */
    @Override
    public BlindBoxProduct selectBlindBoxProductById(Long id) {
        return blindBoxProductMapper.selectBlindBoxProductById(id);
    }

    /**
     * 查询盲盒商品列表
     *
     * @param blindBoxProduct 盲盒商品
     * @return 盲盒商品
     */
    @Override
    public List<BlindBoxProduct> selectBlindBoxProductList(BlindBoxProduct blindBoxProduct) {
        return blindBoxProductMapper.selectBlindBoxProductList(blindBoxProduct);
    }

    /**
     * 新增盲盒商品
     *
     * @param blindBoxProduct 盲盒商品
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int insertBlindBoxProduct(BlindBoxProduct blindBoxProduct) {
        List<BlindBoxProduct> blindBoxProducts = blindBoxProductMapper.selectProductListsByBlidBoxId(blindBoxProduct.getBlindBoxId());
        if (!CollectionUtils.isEmpty(blindBoxProducts)) {
            //查询有几个箱子
            List<Box> boxes = boxMapper.selectListByBlindBoxId(blindBoxProduct.getBlindBoxId());
            for (Box box : boxes) {
                List<BlindBoxProduct> insertBlindBoxProducts = new ArrayList<>(blindBoxProducts);
                log.info("商品:{}", JSON.toJSONString(insertBlindBoxProducts));
                for (BlindBoxProduct insertBlindBoxProduct : insertBlindBoxProducts) {
                    insertBlindBoxProduct.setBoxId(box.getId());
                    blindBoxProductMapper.insertBlindBoxProduct(insertBlindBoxProduct);
                }
            }
        }
        if (blindBoxProduct.getPublicProductId() != null) {
            BlindBoxPublicProduct blindBoxPublicProduct = blindBoxPublicProductMapper.selectBlindBoxPublicProductById(blindBoxProduct.getPublicProductId());
            if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                throw new RuntimeException("请选择商品");
            }
            blindBoxProduct.setExcessCount(blindBoxProduct.getTotalCount());
            blindBoxProduct.setClassId(blindBoxPublicProduct.getClassId());
            blindBoxProduct.setName(blindBoxPublicProduct.getName());
            blindBoxProduct.setPicture(blindBoxPublicProduct.getPicture());
            blindBoxProduct.setSeriesName(blindBoxPublicProduct.getSeriesName());
            blindBoxProduct.setPrice(blindBoxPublicProduct.getPrice());
            blindBoxProduct.setIsSplit(blindBoxPublicProduct.getIsSplit());
            blindBoxProduct.setSplitCount(blindBoxPublicProduct.getSplitCount());
            blindBoxProduct.setIsDelivery(blindBoxPublicProduct.getIsDelivery());
            blindBoxProduct.setFreight(blindBoxPublicProduct.getFreight());
        }
        blindBoxProduct.setCreateTime(DateUtils.getNowDate());
        return blindBoxProductMapper.insertBlindBoxProduct(blindBoxProduct);
    }

    /**
     * 批量删除盲盒商品
     *
     * @param ids 需要删除的盲盒商品主键
     * @return 结果
     */
    @Override
    public int deleteBlindBoxProductByIds(Long[] ids) {
        return blindBoxProductMapper.deleteBlindBoxProductByIds(ids);
    }

    /**
     * 删除盲盒商品信息
     *
     * @param id 盲盒商品主键
     * @return 结果
     */
    @Override
    public int deleteBlindBoxProductById(Long id) {
        return blindBoxProductMapper.deleteBlindBoxProductById(id);
    }

    @Override
    public List<BlindBoxProduct> selectNotBlindList(BlindBoxProduct blindBoxProduct) {
        return blindBoxProductMapper.selectNotBlindList();
    }

    /**
     * 修改盲盒商品
     *
     * @param blindBoxProduct 盲盒商品
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int updateBlindBoxProduct(BlindBoxProduct blindBoxProduct) {
        //先查询盲盒中所有绑定的商品
        List<BlindBoxProduct> blindBoxProducts = blindBoxProductMapper.selectList(blindBoxProduct.getId());
        if (!CollectionUtils.isEmpty(blindBoxProducts)) {
            for (BlindBoxProduct boxProduct : blindBoxProducts) {
                BlindBoxProduct blindBoxProduct1 = blindBoxProductMapper.selectBlindBoxProductById(boxProduct.getId());
                if (boxProduct.getPublicProductId() != null && !boxProduct.getPublicProductId().equals(blindBoxProduct1.getPublicProductId())) {
                    BeanUtils.copyProperties(blindBoxProduct, boxProduct);
                    BlindBoxPublicProduct blindBoxPublicProduct = blindBoxPublicProductMapper.selectBlindBoxPublicProductById(boxProduct.getPublicProductId());
                    if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                        throw new RuntimeException("请选择商品");
                    }

                    boxProduct.setClassId(blindBoxPublicProduct.getClassId());
                    boxProduct.setName(blindBoxPublicProduct.getName());
                    boxProduct.setPicture(blindBoxPublicProduct.getPicture());
                    boxProduct.setSeriesName(blindBoxPublicProduct.getSeriesName());
                    boxProduct.setPrice(blindBoxPublicProduct.getPrice());
                    boxProduct.setIsSplit(blindBoxPublicProduct.getIsSplit());
                    boxProduct.setSplitCount(blindBoxPublicProduct.getSplitCount());
                    boxProduct.setIsDelivery(blindBoxPublicProduct.getIsDelivery());
                    boxProduct.setFreight(blindBoxPublicProduct.getFreight());
                }

                boxProduct.setUpdateTime(DateUtils.getNowDate());
                blindBoxProductMapper.updateBlindBoxProduct(boxProduct);
            }
        }
        return 1;
    }
}
